// pages/random/result.js
const db = wx.cloud.database()
var app = getApp();
Page({
  data: {
    openid: '',
    exercise: null,
    userRandomTest: null,
    ringRight: 0,
    ringLight: 0,
  },
  compute: function(corrects,errors){
    let degree = corrects / (corrects + errors) * 360
    let ringRight = degree > 180 ? 180 : degree
    let ringLeft = degree > 180 ? degree % 180 : 0
    this.setData({
      ringRight,
      ringLeft
    })
  },
  tomistake: function(){
    wx.redirectTo({
      url: '../mistake/mistake',
    })
  },
  onLoad: function (options) {
    var that = this
    var openid = wx.getStorageSync('openid')
    if(!openid){
      wx.navigateTo({
        url: '../index/welcome',
      })
      return false
    }
    that.setData({
      openid
    })
    var _id = options._id
    wx.showLoading({
      title: '加载中',
    })
    if(_id){
      db.collection('userRandomTest').doc(_id).get({
        success: res => {
          that.setData({
            userRandomTest: res.data
          })
          that.compute(res.data.corrects,res.data.errors)
        }
      })
      
    }else{
      wx.navigateTo({
        url: '../index/index',
      })
    }
    wx.hideLoading()
  },

  onShow: function(){

  },
})